package test.service;

import com.alibaba.excel.EasyExcel;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import test.Utils.ObjectUtil;
import test.common.DataResult;
import test.vo.ExportYqfkdj;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @ project name : example
 * @ Author: XuLeHuang
 * @ Date: 2022/10/1 18:34
 * @ Description:
 */
@Service
public class YqfkdjService {
    private static List<ExportYqfkdj> dataList = new ArrayList<ExportYqfkdj>();
    private static ExportYqfkdj exportYqfkdj = new ExportYqfkdj();

    static {
        for (int i = 0 ; i < 50 ; i++) {
            exportYqfkdj.setId(String.valueOf(i));
            exportYqfkdj.setName("许乐煌");
            exportYqfkdj.setAge("22");
            exportYqfkdj.setSex("男");
            exportYqfkdj.setEmail("18736183@qq.com");
            exportYqfkdj.setAddr("哥谭");
            dataList.add(exportYqfkdj);
        }
    }
    @Transactional(rollbackFor = { Exception.class })
    public DataResult exporYqfkdj() throws Exception {
        DataResult result = new DataResult();
        result.setResults(dataList);
        List<ExportYqfkdj> list = new ArrayList<ExportYqfkdj>();
        try {
            /* 查询导出信息 */
            SimpleDateFormat df = new SimpleDateFormat("yyyyMMddhhmmssSSS");
            for (int i = 0; i < 50; i++) {
                ExportYqfkdj dmKhfwdcDtjlZxDto = new ExportYqfkdj();
                dmKhfwdcDtjlZxDto = dataList.get(i);
                list.add(dmKhfwdcDtjlZxDto);
            }
            String filepath = "D:/" + df.format(new Date()) + ".xlsx";
            EasyExcel.write(filepath, ExportYqfkdj.class).head(head()).sheet().doWrite(list);
            result.setResults(list);
            result.setSuccess(true);
            result.setMsg(filepath);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMsg("导出失败");
            e.printStackTrace();
            throw e;
        }
        return result;
    }

    /**
     * 用户信息导出表头
     * @author zhouhong
     * @return List<List<String>>
     */
    private List<List<String>> head() {
        List<List<String>> list = new ArrayList<List<String>>();
        List<String> head0 = new ArrayList<String>();
        head0.add("序号");
        List<String> head1 = new ArrayList<String>();
        head1.add("姓名");
        List<String> head2 = new ArrayList<String>();
        head2.add("年龄");
        List<String> head3 = new ArrayList<String>();
        head3.add("性别");
        List<String> head4 = new ArrayList<String>();
        head4.add("邮箱");
        List<String> head5 = new ArrayList<String>();
        head5.add("住址");
        list.add(head0);
        list.add(head1);
        list.add(head2);
        list.add(head3);
        list.add(head4);
        list.add(head5);
        return list;
    }
}
